<?php
namespace app\madmin\controller;
use app\madmin\model\MallConfigModel;

/**
 * 商品配置控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallConfig extends PublicBase{

	# 商品快捷配置方法
	public function showBase(){
        $MallConfig = new MallConfigModel;

		if(request()->isPost()){
            $Config = input('post.');

            if(!empty($Config)){
                # 循环更新配置
                foreach($Config as $k => $v){
                    $Where = [];
                    $Where['config_name'] = $k;
                    $Where['config_show'] = 1;
                    $FindConfig = $MallConfig->mInfo($Where);
                    if(!empty($FindConfig)){
                        if($FindConfig['config_value'] != $v){
                            $MallConfig->mUpdate($Where, ['config_value'=>$v]);
                        }
                    }
                }
            }

            # 更新配置缓存
            $this->WechatConfigLists();

            $this->ShowData(1, lang('XtTsGxCg'), url('MallConfig/showBase'));
        }else{
            # 获取配置栏目
            $ListGroup = $this->getLexiconCache('MALL_CONFIG_GROUP');

            $ListLists = $MallConfig->mAll(['config_show'=>1]);

            $Group = [];
            $Lists = [];
            if(!empty($ListGroup)){
                # 过滤配置空闲栏目
                foreach($ListGroup as $k => $v){
                    $GroupCount = $MallConfig->mCount(['config_group'=>$v['content_value'], 'config_show'=>1]);
                    if($GroupCount > 0){
                        $Group[$k]['group_type'] = $v['content_value'];
                        $Group[$k]['group_name'] = $v['content_name'];
                    }
                }

                # 循环配置添加分组
                foreach($Group as $k => $v){
                    $Lists[$k] = $v;
                    foreach($ListLists as $i => $z){
                        if($v['group_type'] == $z['config_group']){
                            if($z['config_type'] == 2){
                                if(!empty($z['config_select'])){
                                    $ListLists[$i]['config_select'] = explode(',', $z['config_select']);
                                }
                            }
                            $Lists[$k]['group_list'][] = $z;
                            unset($z);
                        }
                    }
                }
            }

            # 模版赋值
            return $this->fetch('MallConfig/showBase', [
                'Group' => $Group,
                'Lists' => $Lists
            ]);
        }
	}

    # 商品配置列表方法
    public function showList(){
        $MallConfig = new MallConfigModel;

        $Where = $this->getFormWhere();

        $config_group = input('param.config_group');
        if(!empty($config_group)){
            $Where['config_group'] = $config_group;
        }
        $this->assign('config_group', $config_group);

        $config_type = input('param.config_type');
        if(!empty($config_type)){
            $Where['config_type'] = $config_type;
        }
        $this->assign('config_type', $config_type);

        $Lists = $MallConfig->mList($Where, '*', config('CNF_LISTPAGE'));

        # 模版赋值
        return $this->fetch('MallConfig/showList', [
            'Lists' => $Lists,
            'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
        ]);
    }

    # 商品配置新增方法
    public function showAdd(){
        $MallConfig = new MallConfigModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallConfig->mAdd($Param);

            # 更新配置缓存
            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            # 模版赋值
            return $this->fetch('MallConfig/showAdd', [
                'Info'  => Null,
                'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
            ]);
        }
    }

    # 商品配置更新方法
    public function showEdit(){
        $MallConfig = new MallConfigModel;

        $ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallConfig->mEdit(['config_id'=>$ID], $Param);

            # 更新配置缓存
            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $MallConfig->mInfo(['config_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }else{
                # 模版赋值
                return $this->fetch('MallConfig/showAdd', [
                    'Info'  => $Info,
                    'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
                ]);
            }
        }
    }

    # 商品配置删除方法
    public function showDel(){
        if(request()->isPost()){
            $MallConfig = new MallConfigModel;

            $ID = input('post.id');

            $Back = $MallConfig->mDel(['config_id'=>$ID]);

            # 更新配置缓存
            $this->getConfigList();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }
    }

}